/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */
/*
	By Lief
 */

package team.mai.antexpress.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import team.mai.antexpress.entities.DepartmentEntity;
import team.mai.antexpress.services.DepartmentService;

import java.io.Serializable;
import java.util.List;

@SuppressWarnings("Duplicates")
@Repository
public class DepartmentDAO {
	@Autowired
	SessionFactory sessionFactory;

	public Integer insertDepartment(DepartmentEntity dept) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			Serializable save = session.save(dept);
			transaction.commit();
			return (Integer) save;
		} catch (Exception ex) {
			ex.printStackTrace();
			return DepartmentService.ERROR;
		}
	}

	public boolean deleteDepartment(int deptId) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.delete(session.get(DepartmentEntity.class, deptId));
			transaction.commit();
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}

	public boolean hasUserInDepartment(int deptId) {
		try (Session session = sessionFactory.openSession()) {
			DepartmentEntity departmentEntity = session.get(DepartmentEntity.class, deptId);
			Query query = session.createQuery("FROM UserEntity WHERE departmentId=:dept");
			query.setParameter("dept", deptId);
			return query.list().size() > 0;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean deleteDepartments(List<Integer> ids) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			for (Integer i : ids) {
				DepartmentEntity departmentEntity = session.get(DepartmentEntity.class, i);
				session.delete(departmentEntity);
			}
			transaction.commit();
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}

	public boolean updateDepartment(DepartmentEntity dept) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.update(dept);
			transaction.commit();
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}

	public DepartmentEntity getDepartment(int id) {
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM DepartmentEntity where id=:id");
			query.setParameter("id", id);
			List<DepartmentEntity> list = query.list();
			if (!list.isEmpty())
				return list.get(0);
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}

	}

	public DepartmentEntity getDepartment(String name) {
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM DepartmentEntity where name=:name");
			query.setParameter("name", name);
			List<DepartmentEntity> list = query.list();
			if (!list.isEmpty())
				return list.get(0);
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public List<DepartmentEntity> getAllDepartments() {
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM DepartmentEntity");
			List<DepartmentEntity> list = query.list();
			return list;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public boolean ifExistsDepartment(String except, String deptName) {
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM DepartmentEntity WHERE name=:deptName");
			query.setParameter("deptName", deptName);
			List list = query.list();
			if (list.isEmpty()) {
				return false;
			} else {
				if (((DepartmentEntity) list.get(0)).getName().equals(except)) {
					return false;
				} else {
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}

	public boolean deleteUsersInDepartment(int deptId) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			Query query = session.createQuery("DELETE FROM UserEntity WHERE departmentId=:dept");
			query.setParameter("dept", deptId);
			query.executeUpdate();
			transaction.commit();
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
}
